<template>
  <!-- 上传厂家证照的表格modal -->
  <div>
    <basic-modal :modalOpt="modal" :buttonText="['', '取消']" @closeModal="closeModal" :footer="footer">
      <template #cusBtn>
        <a-button type="primary" :loading="loading_p" @click="onPassed">{{ isview ? '通过' : '批量通过' }}</a-button>
        <a-button type="primary" @click="onUnPassed">{{ isview ? '驳回' : '批量驳回' }}</a-button>
      </template>
      <template>
        <p>操作后请提醒客户查看！</p>
        <batch-operat-btn v-if="editable" :isView="false" :addData="true" @onAddData="onAddData">
          <template #cusBtn> </template>
        </batch-operat-btn>
        <a-list-info v-if="isview" span="12" :infoDatas="infoDatas" />
        <div class="recordBox" v-if="isview == 'view'">
          <a-list-table
            :columns="columns"
            :tableData="listdata"
            :isSelecttion="false"
            :loading="loading"
            :scroll="{ y: null }"
          >
          </a-list-table>
        </div>
      </template>
    </basic-modal>
    <div>
      <basic-modal :modalOpt="reason"  :buttonText="['', '取消']" @closeModal="onCancel" :footer="footer">
        <template #cusBtn>
          <a-button type="primary" @click="onSure" :loading="loadingr">确认</a-button>
        </template>
        <template>
          <a-textarea v-model="reasonVal" placeholder="请输入驳回理由" />
        </template>
      </basic-modal>
    </div>
    
  </div>
</template>

<script>
import basicModal from '@/views/app/biz/common/basicModal'
import aListTable from '@/views/app/biz/common/aListTable'
import aListInfo from '@/views/app/biz/common/aListInfo'
import batchOperatBtn from '@/views/app/biz/common/batchOperatBtn'

// import { getInfoByPid as getSupplierInfoByPid } from '@/views/app/biz/supplier/supplier'
import { getInfoByPid, pushInfo2 } from './purchasedgoods'
import { getZZImgByzzpid } from '@/views/app/biz/licenseImg/licenseImg'

export default {
  components: {
    basicModal,
    aListTable,
    batchOperatBtn,
    aListInfo
  },
  data() {
    return {
      footer: undefined,
      editable: false,
      loading: false,
      isbatch: false,
      isview: true,
      reason: {
        modalVisible: false,
        modalTitle: '请填写驳回理由',
        modalWidth: '400px',
        zIndex:1001
      },
      loadingr: false,
      loading_p: false,
      reasonVal: '',
      modal: {
        modalVisible: false,
        modalTitle: '审核证照',
        modalWidth: '1000px'
      },
      infoDatas: [
        { title: '数据有效区域', id: 'areaUse', value: '' },
        { title: '自编编码', id: 'code', value: '' },
        { title: 'EDI编码', id: 'ediCode', value: '' },
        { title: '区域内编码', id: 'standardCode', value: '' },
        { title: '医保代码', id: 'medicareCode', value: '' },
        { title: '省医保代码', id: 'medicareCode2', value: '' },
        { title: '市医保代码', id: 'medicareCode3', value: '' },
        { title: '一级分类', id: 'first', value: '' },
        { title: '二级分类', id: 'second', value: '' },
        { title: '三级分类', id: 'third', value: '' },
        { title: '品名', id: 'productNameZh', value: '' },
        { title: '英文名称', id: 'productNameEn', value: '' },
        { title: '通用名', id: 'commonName', value: '' },
        { title: '规格', id: 'spec', value: '' },
        { title: '型号', id: 'model', value: '' },
        { title: '描述', id: 'disc', value: '' },
        { title: '品牌', id: 'brand', value: '' },
        { title: '上市许可持有企业社会编码', id: 'mahCode', value: '' },
        { title: '上市许可持有企业中文名', id: 'mahZh', value: '' },
        { title: '上市许可持有企业英文名', id: 'mahEn', value: '' },
        { title: '生产企业社会编码', id: 'manufacturerCode', value: '' },
        { title: '生产企业中文名称', id: 'manufacturerZh', value: '' },
        { title: '生产企业英文名称', id: 'manufacturerEn', value: '' },
        { title: '产地', id: 'origin', value: '' },
        { title: '产品类别', id: 'category', value: '' },
        { title: '计量单位', id: 'measurement', value: '' },
        { title: '药品本位码', id: 'sdc', value: '' },
        { title: '当前批准类型', id: 'approvalType', value: '' },
        { title: '当前注册证号', id: 'approvalNum', value: '' },
        { title: '当前有效期始', id: 'approvalDate', value: '' },
        { title: '当前有效期止', id: 'validDate', value: '' },
        { title: '状态', id: 'state', value: '' },
      ],
      columns: [
        { title: '证照类型', dataIndex: 'zhengZhaolxmc', key: 'zhengZhaolxmc', align: 'center' },
        { title: '证照说明', dataIndex: 'zhengZhaosm', key: 'zhengZhaosm', align: 'center' },
        {
          title: '证照图片',
          dataIndex: 'zhengZhaotpurl',
          key: 'zhengZhaotpurl',
          align: 'center',
          scopedSlots: { customRender: 'img' }
        }
      ],
      listdata: [],
      listKey: 0,
      pid: '',
      zzPid: '',
      sysSeq: 0,
      aRecord: {},
      recordArr: [],
      loadings: false,
      customerOrgCode:'',
      supplierOrgCode:''
    }
  },
  computed: {},
  mounted() {},
  methods: {
    // 点击了上传
    addModal(val) {
      this.modal.modalVisible = true
      this.modal.modalTitle = '审核商品'
      this.pid = val
      this.getSBypid(val)
    },
    // 点击了审核
    editModal(val, val2) {
      this.pid = val
      this.zzPid = val2
      this.modal.modalVisible = true
      this.modal.modalTitle = '审核商品'
      this.getSBypid(val)
      // this.getZZImgBypid(val2)
    },
    // 点击了批量审核
    batchModal(val) {
      this.recordArr = JSON.parse(JSON.stringify(val))
      this.modal.modalVisible = true
      this.modal.modalTitle = '批量审核商品'
      this.modal.modalWidth = '400px'
      this.isview = false
      this.isbatch = true
    },
    // 点击了全部审核
    allModal(val) {
      this.recordArr =[]
      this.customerOrgCode = val.customerOrgCode
      this.supplierOrgCode = val.supplierOrgCode
      this.modal.modalVisible = true
      this.modal.modalTitle = '一键审核'
      this.modal.modalWidth = '400px'
      this.isview = false
      this.isbatch = true
    },
    // 关闭模态详细框
    closeModal() {
      this.onCloseModal()
      this.$emit('close')
    },
    closeModal2() {
      this.onCloseModal()
      this.$emit('ok')
    },
    onCloseModal(){
      // this.modal.modalVisible = false
      // this.listdata = []
      Object.assign(this.$data, this.$options.data());
    },
    //
    onAddData() {
      this.$refs.addSellerLicChildModal.addModal(this.pid)
    },

    // 审核通过
    onPassed() {
      this.loading_p = true
      let datas={
        state: "已审核",
        remark:null,
        customerOrgCode: this.customerOrgCode,
        supplierOrgCode: this.supplierOrgCode
      }
      if (this.isbatch) {
        if (!this.recordArr.length) {
          datas.pids='all'
        }else{
          datas.pids=''
          for (const item of this.recordArr) {
            datas.pids+=item.pid+','
          }
        }
      } else {
        datas.pids=this.pid
      }
      console.log(datas)
      this.saveObjInfo(datas)
    },
    // 审核不通过onUnPassed
    onUnPassed(val) {
      this.reason.modalVisible = true
    },
    onSure() {
      this.loadingr = true
      let datas={
        state: "已驳回",
        remark:this.reasonVal,
        customerOrgCode: this.customerOrgCode,
        supplierOrgCode: this.supplierOrgCode
      }
      if (this.isbatch) {
        if (!this.recordArr.length) {
          datas.pids='all'
        }else{
          datas.pids=''
          for (const item of this.recordArr) {
            datas.pids+=item.pid+','
          }
        }
      } else {
        datas.pids=this.pid
      }
      this.saveObjInfo(datas)
    },
    onCancel() {
      this.loadingr = false
      this.loading_p = false
      this.reasonVal=null
      this.reason.modalVisible = false
    },
    // 保存详细信息
    async saveObjInfo(val) {
      console.log(val);
      let res = await pushInfo2(val)
      console.log(res);
      if (res && res.code && res.code === 'C1' && res.data) {
        this.$message.success({ content:'审核成功', duration: 3 })
      } else {
        this.$message.error({ content:'审核失败，原因：'+res.message, duration: 3 })
      }
      this.onCancel()
      this.closeModal2()
    },
    // 保存详细信息
    dealResult(reqArr, isPass) {
      let tips = ''
      if (isPass) {
        tips = '已审核通过'
      } else {
        tips = '已成功驳回'
      }
      return Promise.all(reqArr)
        .then(res => {
          this.loading = false
          console.log(res)
          for (const item of res) {
            if (item && item.code && item.code === 'C1' && item.data) {
              this.$message.success({ content: '商品:' + item.data.pinMing + tips, duration: 2 })
            } else {
              this.$message.success({ content: '商品:' + item.data.pinMing + '审核失败，请稍后再试', duration: 2 })
            }
          }
          this.onCancel()
          this.closeModal2()
        })
        .catch(err => {
          console.log(err)
          this.$message.error({ content: '请求出错:' + err, duration: 3 })
          this.onCancel()
        })
    },
    // 得到初始化信息
    async getSBypid(val) {
      let res = await getInfoByPid(val)
      if (res && res.code && res.code === 'C1') {
        this.sysSeq = res.data.sysSeq
        this.aRecord = res.data
        for (const key in res.data) {
          for (const item of this.infoDatas) {
            if (key == item.id) {
              item.value = res.data[key]
            }
          }
        }
      }
    },
    // 得到初始化证照图片信息
    async getZZImgBypid(val2) {
      this.loading = true
      let pa = {
        zhengZhaoPid: val2
      }
      let res = await getZZImgByzzpid(pa)
      if (res && res.code && res.code === 'C1' && res.data) {
        const resdata = res.data
        for (let item of resdata) {
          this.listdata.push({
            key: item.pid,
            ...item
          })
        }
        this.loading = false
      }
      console.log(res)
    }
  }
}
</script>

<style>
.recordBox {
  width: 100%;
  min-height: 80px;
}
</style>
